package com.ruoyi.duty.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.duty.domain.bo.DutyGzrzBo;
import com.ruoyi.duty.domain.bo.DutyRzFullBo;
import com.ruoyi.duty.domain.vo.DutyGzrzVo;
import com.ruoyi.duty.domain.vo.DutyRzFullVo;
import com.ruoyi.duty.service.IDutyGzrzService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 工作日志
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/duty/gzrz")
public class DutyGzrzController extends BaseController {

    private final IDutyGzrzService iDutyGzrzService;

    @PostMapping("queryRzFull")
    public R<List<DutyRzFullVo>> queryRzFull(@RequestBody DutyRzFullBo query) {
        return R.ok(iDutyGzrzService.queryRzFull(query));
    }

    @PostMapping("query")
    public R<List<DutyGzrzVo>> query(@RequestBody DutyGzrzBo bo) {
        return R.ok(iDutyGzrzService.queryList(bo));
    }

    /**
     * 查询工作日志列表
     */
    @GetMapping("/list")
    public TableDataInfo<DutyGzrzVo> list(DutyGzrzBo bo, PageQuery pageQuery) {
        return iDutyGzrzService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工作日志列表
     */
    @Log(title = "工作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DutyGzrzBo bo, HttpServletResponse response) {
        List<DutyGzrzVo> list = iDutyGzrzService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作日志", DutyGzrzVo.class, response);
    }

    /**
     * 获取工作日志详细信息
     *
     * @param gzrzId 主键
     */
    @GetMapping("/{gzrzId}")
    public R<DutyGzrzVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long gzrzId) {
        return R.ok(iDutyGzrzService.queryById(gzrzId));
    }

    /**
     * 新增工作日志
     */
    @SaCheckPermission("duty:gzrz:add")
    @Log(title = "工作日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DutyGzrzBo bo) {
        return toAjax(iDutyGzrzService.insertByBo(bo));
    }

    /**
     * 修改工作日志
     */
    @SaCheckPermission("duty:gzrz:edit")
    @Log(title = "工作日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DutyGzrzBo bo) {
        return toAjax(iDutyGzrzService.updateByBo(bo));
    }

    /**
     * 删除工作日志
     *
     * @param gzrzIds 主键串
     */
    @SaCheckPermission("duty:gzrz:remove")
    @Log(title = "工作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gzrzIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] gzrzIds) {
        return toAjax(iDutyGzrzService.deleteWithValidByIds(Arrays.asList(gzrzIds), true));
    }
}
